//
//  DKAuthFeedBackAlertView.m
//  DanKU
//
//  Created by 蓝鳍互娱 on 2023/12/1.
//

#import "DKAuthFeedBackAlertView.h"
@interface DKAuthFeedBackAlertView()<UITextViewDelegate>
@property (nonatomic, weak)zhPopupController * dk_AuthStartAlertViewPop;
@property (nonatomic,strong)DKAuthFeedBackAlertView *dk_AuthFeedBackAlertView;
@property (nonatomic, strong)UITextView * feedBackTextView;
@property (nonatomic, strong)UILabel * feedBackNumLab;
@property (nonatomic, strong)UIButton * feedBackSubimBtn;
@property (nonatomic,strong) UIButton *feedBackKeFuBtn;
@property (nonatomic,strong) DKCustomerServiceModel * customerServiceMdl;//kefu
@property (nonatomic, copy)void(^SureAuthFeedBackAlertBlock)(void);
@end
@implementation DKAuthFeedBackAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 50;
        self.clipsToBounds = YES;
        [self dk_addSubView];
        [self checkKeFu];
    }
    return self;
}
- (void)dk_addSubView{
    UILabel *dk_AuthFeedBackHeadLab = [[UILabel alloc] init];
    dk_AuthFeedBackHeadLab.textColor = DK_Hex(@"#333333");
    dk_AuthFeedBackHeadLab.font = DK_FONT_Semibold(17);
    dk_AuthFeedBackHeadLab.text = @"Feedback";
    [self addSubview:dk_AuthFeedBackHeadLab];
    [dk_AuthFeedBackHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
        make.centerX.equalTo(self);
    }];
    UILabel *dk_AuthFeedBackTitleLab = [[UILabel alloc] init];
    dk_AuthFeedBackTitleLab.textColor = DK_Hex(@"#333333");
    dk_AuthFeedBackTitleLab.font = DK_FONT_Regular(14);
    dk_AuthFeedBackTitleLab.numberOfLines = 0;
    dk_AuthFeedBackTitleLab.text = @"Please describe your feedback to us";
    dk_AuthFeedBackTitleLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:dk_AuthFeedBackTitleLab];
    [dk_AuthFeedBackTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dk_AuthFeedBackHeadLab.mas_bottom).offset(DK_IsWidthScale(19));
        make.height.equalTo(@(DK_IsWidthScale(20)));
        make.left.equalTo(@(DK_IsWidthScale(10)));
        make.right.equalTo(@(-DK_IsWidthScale(10)));
    }];
    UIView *dk_FeedBackDescribeView = [[UIView alloc] init];
    dk_FeedBackDescribeView.backgroundColor = DK_Hex(@"#FAFAFA");
    dk_FeedBackDescribeView.layer.cornerRadius = DK_IsWidthScale(10);
    [self addSubview:dk_FeedBackDescribeView];
    [dk_FeedBackDescribeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(22)));
        make.right.equalTo(@(-DK_IsWidthScale(22)));
        make.top.equalTo(dk_AuthFeedBackTitleLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.height.equalTo(@(DK_IsWidthScale(140)));
    }];
    //占位符
    self.feedBackTextView = [[UITextView alloc] init];
    self.feedBackTextView.textColor = DK_Hex(@"#333333");
    self.feedBackTextView.font = DK_FONT_Regular(14);
    self.feedBackTextView.backgroundColor = UIColor.clearColor;
    self.feedBackTextView.delegate = self;
    [dk_FeedBackDescribeView addSubview:self.feedBackTextView];
    [self.feedBackTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(10)));
        make.right.equalTo(@(-DK_IsWidthScale(10)));
        make.top.equalTo(@(DK_IsWidthScale(10)));
        make.bottom.equalTo(@(-DK_IsWidthScale(30)));
    }];
    UILabel* feedBackPlaceHolderLab = [[UILabel alloc] init];
    feedBackPlaceHolderLab.textColor = DK_Hex(@"#CCCCCC");
    feedBackPlaceHolderLab.font = DK_FONT_Regular(14);
    feedBackPlaceHolderLab.text = @"Please enter";
    [feedBackPlaceHolderLab sizeToFit];
    [self.feedBackTextView addSubview:feedBackPlaceHolderLab];
    [self.feedBackTextView setValue:feedBackPlaceHolderLab forKey:@"_placeholderLabel"];
    
    self.feedBackNumLab = [[UILabel alloc] init];
    self.feedBackNumLab.font = DK_FONT_Regular(14);
    self.feedBackNumLab.textColor = DK_Hex(@"#CCCCCC");
    self.feedBackNumLab.text = @"0/200";
    [dk_FeedBackDescribeView addSubview:self.feedBackNumLab];
    [self.feedBackNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(10)));
        make.bottom.equalTo(@(-DK_IsWidthScale(10)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    //next
    self.feedBackSubimBtn = [[UIButton alloc] init];
    [self.feedBackSubimBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [self.feedBackSubimBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.feedBackSubimBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.feedBackSubimBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.feedBackSubimBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.feedBackSubimBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.feedBackSubimBtn.enabled = NO;
    [self.feedBackSubimBtn addTarget:self action:@selector(submitFeedBack) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.feedBackSubimBtn];
    [self.feedBackSubimBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(dk_FeedBackDescribeView.mas_bottom).offset(DK_IsWidthScale(10));
    }];
    UILabel *dk_AuthFeedBackKeFuLab = [[UILabel alloc] init];
    dk_AuthFeedBackKeFuLab.textColor = DK_Hex(@"#333333");
    dk_AuthFeedBackKeFuLab.font = DK_FONT_Regular(14);
    dk_AuthFeedBackKeFuLab.numberOfLines = 0;
    dk_AuthFeedBackKeFuLab.text = @"You can also call our customer service hotline directly";
    [self addSubview:dk_AuthFeedBackKeFuLab];
    [dk_AuthFeedBackKeFuLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.feedBackSubimBtn.mas_bottom).offset(DK_IsWidthScale(10));
        make.left.equalTo(@(DK_IsWidthScale(23)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
    }];
    
    UIView *dk_FeedBackKeFuView = [[UIView alloc] init];
    dk_FeedBackKeFuView.backgroundColor = DK_Hex(@"#F7F7F8");
    dk_FeedBackKeFuView.layer.cornerRadius = DK_IsWidthScale(14);
    dk_FeedBackKeFuView.clipsToBounds = YES;
    dk_FeedBackKeFuView.layer.borderColor = DK_Hex(@"#4566FF").CGColor;
    dk_FeedBackKeFuView.layer.borderWidth = 0.5;
    [self addSubview:dk_FeedBackKeFuView];
    [dk_FeedBackKeFuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_AuthFeedBackKeFuLab.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.feedBackKeFuBtn = [[UIButton alloc] init];
    [self.feedBackKeFuBtn setTitleColor:DK_Hex(@"#333333") forState:UIControlStateNormal];
    self.feedBackKeFuBtn.titleLabel.font = DK_FONT_Semibold(16);
    [dk_FeedBackKeFuView addSubview:self.feedBackKeFuBtn];
    [self.feedBackKeFuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(60)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    UIButton * feedBackKeFuCopyBtn = [[UIButton alloc] init];
    [feedBackKeFuCopyBtn setTitle:@"Copy" forState:UIControlStateNormal];
    [feedBackKeFuCopyBtn setBackgroundImage:DK_GetImage(@"dk_AuthCopyBg") forState:UIControlStateNormal];
    [feedBackKeFuCopyBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    feedBackKeFuCopyBtn.titleLabel.font = DK_FONT_Semibold(16);
    [feedBackKeFuCopyBtn addTarget:self action:@selector(copyKeFu) forControlEvents:UIControlEventTouchUpInside];
    [dk_FeedBackKeFuView addSubview:feedBackKeFuCopyBtn];
    [feedBackKeFuCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.feedBackKeFuBtn.mas_right);
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    UIButton * dk_AuthStartCloseBtn = [[UIButton alloc] init];
    [dk_AuthStartCloseBtn setBackgroundImage:DK_GetImage(@"dk_LoginAgreementClose") forState:UIControlStateNormal];
    [dk_AuthStartCloseBtn addTarget:self action:@selector(goCancle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_AuthStartCloseBtn];
    [dk_AuthStartCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
        make.width.equalTo(@(DK_IsWidthScale(17)));
    }];
}
- (void)submitFeedBack{
    if(self.feedBackTextView.text.length > 0){
        WeakSelf(self)
        NSMutableDictionary *feedBackDic = [[NSMutableDictionary alloc] init];
        [feedBackDic setValue:self.feedBackTextView.text forKey:@"content"];
        [feedBackDic setValue:@"feedback" forKey:@"title"];
        [DKPublicHttp apiUserComplaintForAPI:feedBackDic complete:^{
            [DKAlertHelper showMessage:@"Sent successfully"];
            [weakSelf.dk_AuthStartAlertViewPop dismiss];
        }];
    }else{
        [DKAlertHelper showMessage:@"Please enter your feedback"];
    }
}
- (void)checkKeFu{
    WeakSelf(self)
    [DKPublicHttp apiCustomerServiceInfoForAPI:@{} complete:^(DKCustomerServiceModel * _Nonnull customerServiceMdl) {
        weakSelf.customerServiceMdl = customerServiceMdl;
        [weakSelf.feedBackKeFuBtn setTitle:customerServiceMdl.phone forState:UIControlStateNormal];
    }];
}
- (void)copyKeFu{
    if(self.customerServiceMdl){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = self.customerServiceMdl.phone;
        [DKAlertHelper showMessage:@"Copy successfully"];
    }else{
        [DKAlertHelper showMessage:@"Please get customerService"];
    }
}
- (void)goCancle{
    [self.dk_AuthStartAlertViewPop dismiss];
}
+ (void)dk_ShowAuthFeedBackAlertView:(void(^)(void))complete{
    DKAuthFeedBackAlertView * dk_AuthFeedBackAlertView = [[DKAuthFeedBackAlertView alloc] init];
    zhPopupController * sureAuthStartCtrl = [[zhPopupController alloc] initWithView:dk_AuthFeedBackAlertView size:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), DK_IsWidthScale(473))];
    dk_AuthFeedBackAlertView.SureAuthFeedBackAlertBlock = complete;
    sureAuthStartCtrl.presentationStyle = zhPopupSlideStyleTransform;
    sureAuthStartCtrl.layoutType = zhPopupLayoutTypeCenter;
    sureAuthStartCtrl.presentationTransformScale = 0.56;
    sureAuthStartCtrl.dismissonTransformScale = 0.56;
    sureAuthStartCtrl.dismissOnMaskTouched = NO;
    dk_AuthFeedBackAlertView.dk_AuthStartAlertViewPop = sureAuthStartCtrl;
    [sureAuthStartCtrl showInView:((AppDelegate*)([UIApplication sharedApplication].delegate)).window completion:^{
    }];
}
- (void)makeNextStatus:(BOOL)isEnable{
    self.feedBackSubimBtn.enabled = isEnable;
}
#pragma mark -- UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString * textContent = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (textContent.length > 200) {
        return NO;
    }
    if(textContent.length > 0){
        [self makeNextStatus:YES];
    }else{
        [self makeNextStatus:NO];
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    NSInteger maxFontNum = 200;//最大输入限制
    NSString *toBeString = textView.text;
    // 获取键盘输入模式
    NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) { // zh-Hans代表简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > maxFontNum) {
                textView.text = [toBeString substringToIndex:maxFontNum];//超出限制则截取最大限制的文本
                self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/200",maxFontNum];
            } else {
                self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/200",toBeString.length];
            }
        }
    } else {// 中文输入法以外的直接统计
        if (toBeString.length > maxFontNum) {
            textView.text = [toBeString substringToIndex:maxFontNum];
            self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/200",maxFontNum];
        } else {
            self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/200",toBeString.length];
        }
    }
}
@end
